package tianrun.ziguan.api.calculate.config.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections.MapUtils;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

public class MapUtil extends MapUtils {

    public static <K, V> List<V> batchGet(Collection<K> keys, Map<K, V> map) {
        if (CollectionUtil.isEmpty(keys)) {
            return Collections.emptyList();
        }
        return map.entrySet().stream()
                .filter(o -> keys.contains(o.getKey()))
                .map(Map.Entry::getValue).collect(Collectors.toList());
    }

    public static <T> Collection<T> getCollection(Map<String, Object> map, String key, Class<T> claaz) {
        if (map != null) {
            Object answer = map.get(key);
            if (null != answer) {
                return JSONObject.parseArray(JSONObject.toJSONString(answer), claaz);
            }
        }
        return Collections.emptyList();
    }

    public static <T> T getObject(Map<String, Object> map, String key, Class<T> claaz) {
        if (map != null) {
            Object answer = map.get(key);
            if (null != answer) {
                return JSONObject.parseObject(JSONObject.toJSONString(answer), claaz);
            }
        }
        return null;
    }

    public static <T> List<T> getList(Map<String, Object> map, String key, Class<T> claaz) {
        if (map != null) {
            Object answer = map.get(key);
            if (null != answer) {
                return JSONObject.parseArray(JSONObject.toJSONString(answer), claaz);
            }
        }
        return Collections.emptyList();
    }

    public static Collection<String> getCollection(Map<String, Object> map, String key) {
        return getCollection(map, key, String.class);
    }


    public static JSONArray getArray(Map<String, Object> map, String key) {
        if (map != null) {
            Object answer = map.get(key);
            if (Objects.isNull(answer)) {
                return new JSONArray();
            }
            JSON o = (JSON) JSON.toJSON(answer);
            if (o instanceof JSONArray) {
                return (JSONArray) o;
            }
        }
        return new JSONArray();
    }

    public static LocalDateTime getLocalDateTime(Map<String, Object> map, String key) {
        if (map != null) {
            Object answer = map.get(key);
            if (answer != null) {
                return (LocalDateTime) answer;
            }
        }
        return null;
    }

    public static String removeString(Map<String, Object> map, String key) {
        if (map != null) {
            Object answer = map.remove(key);
            if (answer != null) {
                return answer.toString();
            }
        }
        return null;
    }

    public static <T> BiFunction<List<T>, List<T>, List<T>> remappingFunction() {
        return (b, a) -> {
            b.addAll(a);
            return b;
        };
    }
}
